// components/tabbar/tabbar.js
const app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    //tabbar
    navList:[
      {
        name: 'project',
        url: "/pages/equipmentGroup/equipmentGroup"
      },
      {
        name: 'equipment',
        url: "/pages/equipmentList/equipmentList"
      },
      {
        name: 'menu',
        url: "/pages/personalCenter/personalCenter"
      },
      {
        name: 'staff',
        url: "/packageA/pages/userNav/userNav"
      }
    ],
    //当前tabbar
    active: '',
    //当前登录用户
    user:{},
    //主题
    theme:app.globalData.theme,
    //是否显示项目列表
    isShowProject:true
  },

  pageLifetimes: {
    show: function() {
      console.log("app.globalData.userInfo");
      console.log(app.globalData.userInfo);
      //如果是员工角色，不显示项目列表，只显示工作台
      if (app.globalData.userInfo != null){
       let showProject = app.globalData.userInfo.role.includes(40)
        this.setData({
          isShowProject:!showProject
        })
      }
      
      //用户
      this.setData({
        user: app.globalData.userInfo,
      })

      //当前tabbar样式
      this.currentTab();  
    },

    //页面被隐藏
    hide: function() {

    },

    //页面尺寸变化
    resize: function(size) {

    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //当前tabbar样式
    currentTab(){
      let pages = getCurrentPages();
      let currentPage = pages[pages.length - 1];
      let url = `/${currentPage.route}`;

      let navList=this.data.navList;

      navList.forEach(item => {
        if (item.url==url){
          this.setData({
            active: item.name
          });
        }
      });
    },

    //点击tabbar
    onChange(event) {
      let navList=this.data.navList;

      navList.forEach(item => {
        if (item.name==event.detail){
          wx.reLaunch({
            url: item.url
          })
        }
      });
    },

  }
})